package com.wondream.rabbitmq.consumer.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wondream
 * @version 1.0
 * @create 2022/5/8 5:39 PM
 */
@Configuration
public class RabbitmqConfig {
    // 队列名称
    public static final String QUEUE_TOPIC_INFORM_EMAIL = "queue_topic_inform_email_springboot";
    public static final String QUEUE_TOPIC_INFORM_SMS = "queue_topic_inform_sms_springboot";
    public static final String EXCHANGE_TOPIC_INFORM = "exchange_topics_inform_springboot";
    public static final String ROUTINGKEY_EMAIL = "info.#.email.#";
    public static final String ROUTINGKEY_SMS = "info.#.sms.#";

    // 声明交换机
    @Bean(EXCHANGE_TOPIC_INFORM)
    public Exchange getExchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPIC_INFORM).durable(true).build();
    }

    // 声明email队列
    @Bean(QUEUE_TOPIC_INFORM_EMAIL)
    public Queue getEmailQueue(){
        return new Queue(QUEUE_TOPIC_INFORM_EMAIL);
    }
    // 声明sms队列
    @Bean(QUEUE_TOPIC_INFORM_SMS)
    public Queue getSmsQueue(){
        return new Queue(QUEUE_TOPIC_INFORM_SMS);
    }

    // 绑定交换机和email队列
    @Bean
    public Binding bindingEmail(@Qualifier(QUEUE_TOPIC_INFORM_EMAIL) Queue queue,
                                @Qualifier(EXCHANGE_TOPIC_INFORM) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
    }

    // 绑定交换机和sms队列
    @Bean
    public Binding bindingSms(@Qualifier(QUEUE_TOPIC_INFORM_SMS) Queue queue,
                              @Qualifier(EXCHANGE_TOPIC_INFORM) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
    }
}

